package Visualisation;

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

class Ball extends Thread
{

    public Ball(JPanel jpanel)
    {
        x = 0;
        y = 0;
        dx = 2;
        dy = 2;
        box = jpanel;
    }

    public void draw()
    {
        Graphics g = box.getGraphics();
        g.fillOval(x, y, 10, 10);
        g.dispose();
    }

    public void move()
    {
        if(!box.isVisible())
            return;
        Graphics g = box.getGraphics();
        g.setXORMode(box.getBackground());
        g.fillOval(x, y, 10, 10);
        x += dx;
        y += dy;
        Dimension dimension = box.getSize();
        if(x < 0)
        {
            x = 0;
            dx = -dx;
        }
        if(x + 10 >= dimension.width)
        {
            x = dimension.width - 10;
            dx = -dx;
        }
        if(y < 0)
        {
            y = 0;
            dy = -dy;
        }
        if(y + 10 >= dimension.height)
        {
            y = dimension.height - 10;
            dy = -dy;
        }
        g.fillOval(x, y, 10, 10);
        g.dispose();
    }

    public void run()
    {
        try
        {
            draw();
            for(int i = 1; i <= 1000; i++)
            {
                move();
                sleep(5L);
            }

        }
        catch(InterruptedException interruptedexception) { }
    }

    private JPanel box;
    private static final int XSIZE = 10;
    private static final int YSIZE = 10;
    private int x;
    private int y;
    private int dx;
    private int dy;
}
